<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AdminPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'avatar' =>'sometimes|required',
        ];
        //判断验证的数据是更新还是添加,如果是更新就不验证密码,如果是新增就验证密码
        if ($this->method() === "PUT"){
            $rules['name'] = 'sometimes|required';
            $rules['mobile'] = "sometimes|required|regex:/^1[34578][0-9]{9}$/";
            $rules['email'] = 'sometimes|required|email';
             $rules['status'] = 'sometimes|required';

        }else{
            $rules['name'] = 'sometimes|required';
            $rules['mobile'] = "sometimes|required|regex:/^1[34578][0-9]{9}$/";
            $rules['email'] = 'sometimes|required|email';
            $rules['status'] = 'sometimes|required';
            $rules['password'] =  'sometimes|required|min:6|confirmed';
            $rules['password_confirmation'] = 'sometimes|min:6|required';

        }
        return $rules;
    }

    public function messages()
    {
        return[
            'name.required' =>'昵称不能为空',
            'avatar.required' =>'头像不能为空',
            'mobile.required' =>'手机不能为空',
            'email.required' =>'邮箱不能为空',
            'email.email' =>'邮箱不正确',
            'password.required' =>'密码不能为空',
            'password_confirmation.required' =>'确认密码不能为空',
            'password.confirmed' =>'两次密码不一致',
            'status.required' =>'请选择状态',
            'mobile.regex'=>'手机号不正确',
            'password.min'=> '密码不能少于六位',
            'password_confirmation.min'=> '确认密码不能少于六位',
        ];
    }
}
